﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKRevitCurveDrag;
using YJKRevitKernel.BoundingBoxUtility;
using YJKRevitKernel.RevitObjectSerializeUtility;
using YJKRevitView;

namespace YJKRevitArea.AreaThumbnail
{
	[Transaction(TransactionMode.Manual)]
	internal class CmdFirePreventionThumbnail : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Document document = commandData.Application.ActiveUIDocument.Document;
			HideWarningKit @object = new HideWarningKit("DrawFirePreventionThumbnail");
			commandData.Application.Application.FailuresProcessing += @object.OnFailuresProcessing;
			CmdViewType types = CmdViewType.FloorPlan;
			if (SwitchView.SetUsableView(types, commandData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			if (FirePreventionThumbnailManager.GetAllView(document).Count < 1)
			{
				YJKMessageBox.Show("当前文档不存在面积视图");
				return Autodesk.Revit.UI.Result.Failed;
			}
			FormFirePreventionThumbnail formFirePreventionThumbnail = new FormFirePreventionThumbnail(document);
			while (formFirePreventionThumbnail.ShowDialog() == DialogResult.OK)
			{
				int num = 0;
				string text = formFirePreventionThumbnail.cmbScalingRatio.Text;
				for (int i = 0; i < text.Length; i++)
				{
					if (text[i].Equals(':'))
					{
						num++;
					}
				}
				string[] array = formFirePreventionThumbnail.cmbScalingRatio.Text.Split(new char[]
				{
					':'
				});
				List<string> list = new List<string>();
				foreach (string text2 in array)
				{
					if (text2 != "")
					{
						list.Add(text2);
					}
				}
				if (num != 1 || list.Count == 1)
				{
					YJKMessageBox.Show("视图比例输入有误，请重新输入，输入格式为“数字 + : + 数字”，例如:“1:100”,注意“:”是英文输入");
					formFirePreventionThumbnail.cmbScalingRatio.Text = FormFirePreventionThumbnail.ScalingRatio;
					formFirePreventionThumbnail.Hide();
				}
				else
				{
					FirePreventionThumbnailData firepPreventionThumbnailData = formFirePreventionThumbnail.GetFirepPreventionThumbnailData();
					if (-1 == firepPreventionThumbnailData.ViewId)
					{
						YJKMessageBox.Show("所选的视图不存在面积，请重新选择视图");
						formFirePreventionThumbnail.Hide();
					}
					else
					{
						if (formFirePreventionThumbnail.GetSetUpClick() == 0)
						{
							FirePreventionThumbnailSetUpData firePreventionThumbnailSetUpData = this.SetFirePreventionThumbnailSetUpDataOld();
							if (firePreventionThumbnailSetUpData != null)
							{
								firepPreventionThumbnailData.FirePreventionThumbnailSetUpData = firePreventionThumbnailSetUpData;
							}
						}
						List<FirePreventionData> allOldFirePreventionThumbnailData = FirePreventionThumbnailManager.GetAllOldFirePreventionThumbnailData(document, firepPreventionThumbnailData.ViewId);
						if (allOldFirePreventionThumbnailData.Count >= 1)
						{
							XYZ ptSelLocation = null;
							new List<ElementId>();
							try
							{
								List<JigEdgeInfo> list2 = new List<JigEdgeInfo>();
								List<Curve> list3 = new List<Curve>();
								foreach (FirePreventionData firePreventionData in allOldFirePreventionThumbnailData)
								{
									foreach (List<Curve> list4 in FirePreventionThumbnailManager.CreateTransform(firepPreventionThumbnailData.ScalingRatio, DrawFirePreventionThumbnail.DeleteShortLine(firePreventionData.AreaOutline)))
									{
										list3.AddRange(list4);
										foreach (Curve curve in list4)
										{
											list2.Add(new JigEdgeInfo(curve));
										}
									}
								}
								BoundingBoxUV boundingBoxUV = BoundingBoxUtilityMethod.CalculateBonundingBox2D(list3);
								XYZ ptBase = new XYZ(boundingBoxUV.Min.U, boundingBoxUV.Max.V, 0.0);
								new List<JigTextInfo>();
								if (!DragCurveJigInterface.GetInsertPosition(commandData, list2, false, ptBase, ref ptSelLocation))
								{
									return Autodesk.Revit.UI.Result.Succeeded;
								}
							}
							catch (Exception)
							{
								return Autodesk.Revit.UI.Result.Succeeded;
							}
							DrawFirePreventionThumbnail drawFirePreventionThumbnail = new DrawFirePreventionThumbnail(document);
							Transaction transaction = new Transaction(document, "DrawFirePreventionThumbnail");
							try
							{
								transaction.Start();
								drawFirePreventionThumbnail.DrawThumbnail(ptSelLocation, allOldFirePreventionThumbnailData, firepPreventionThumbnailData);
								transaction.Commit();
							}
							catch
							{
								if (transaction.HasStarted())
								{
									transaction.RollBack();
								}
								YJKMessageBox.Show("制作缩略图失败!");
							}
							commandData.Application.Application.FailuresProcessing -= @object.OnFailuresProcessing;
							string text3 = Product.UserDataLocation + "\\FirePreventionThumbnailFormData.xml";
							FirePreventionThumbnailManager.FindFile(text3);
							FirePreventionThumbnailManager.FindFile(this.m_strFilePathSetup);
							Exception ex;
							new HYRevitObjectSerializeManger().TryWriteAPersistentClassByXML(text3, firepPreventionThumbnailData, out ex, true);
							new HYRevitObjectSerializeManger().TryWriteAPersistentClassByXML(this.m_strFilePathSetup, firepPreventionThumbnailData.FirePreventionThumbnailSetUpData, out ex, true);
							break;
						}
						YJKMessageBox.Show("所选的视图不存在面积，请重新选择视图");
						formFirePreventionThumbnail.Hide();
					}
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public FirePreventionThumbnailSetUpData SetFirePreventionThumbnailSetUpDataOld()
		{
			FirePreventionThumbnailSetUpData firePreventionThumbnailSetUpData = null;
			if (File.Exists(this.m_strFilePathSetup))
			{
				firePreventionThumbnailSetUpData = new FirePreventionThumbnailSetUpData();
				Exception ex;
				new HYRevitObjectSerializeManger().TryGetAPersistentClassByXML<FirePreventionThumbnailSetUpData>(this.m_strFilePathSetup, out firePreventionThumbnailSetUpData, out ex, true);
				if (firePreventionThumbnailSetUpData == null)
				{
					return null;
				}
			}
			return firePreventionThumbnailSetUpData;
		}

		private string m_strFilePathSetup = Product.UserDataLocation + "\\FirePreventionThumbnailFormDataSetup.xml";
	}
}
